// pages/phone_detail/phone_detail.js
const app = getApp();
var request = require("../../utils/request.js");
var wx_alert = require("../../utils/wx_alert.js");
var select_b = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    immediate:false,
    choose_list: "true",
    snList:[   
    ],
    upList:[

    ],
    show_have: true,
    select_a: "",
    select_b:[],
    hour:"00",
    mintius:"00",
    second:"00"       
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //快捷导航跳转
    app.bindUser(options);//绑定上下级
    this.turn = request.turn;
    this.close_modal = request.close_modal;
    var that = this;
    request.request(app.globalData.getProductDetail, { productId: options.productId },function(res){
      if(res.code == 0){
        console.log(res)
        var cont_img = res.product.content.split(",")
        that.setData({
          product: res.product,
          store: res.store,
          img_url: request.url + "/",
          cont_img: cont_img,
          productId:options.productId
        })

        that.time(res.product);
      }else{
        wx_alert.alert(res.msg, function () {
          wx.navigateBack({

          })
        })
      }
    })
    request.request(app.globalData.getSnList, { productId: options.productId }, function (res) {
      if (res.code == 0) {
        console.log(res)
        for (var i in res.upList){
          res.upList[i].selected = false
        }
        that.setData({
          snList: res.snPriceList,
          upList: res.upList
        })
      }
    })    


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user_info = wx.getStorageSync('totalInfo');
    return {
      title: "云家故事",
      path: "/pages/store_details/store_details?productId=" + this.data.productId + "&userId=" + user_info.userId 
    }
  },
  now_bespoke:function(){
    var immediate = this.data.immediate;
    var select_a = this.data.select_a;
    var select_b = this.data.select_b;
    
    // 电话
    var store = this.data.store;

    if (immediate){
      immediate = !immediate;
      if (select_a.length || select_b.length>0){
        var send_order = [];
        send_order.push(select_a);
        for (var j in select_b){
          send_order.push(select_b[j]);
        }
        console.log(send_order)
        wx.setStorageSync('send_order', send_order);
        wx.setStorageSync('productId', this.data.productId);
        wx.navigateTo({
          url: '/pages/mine_center/modify_time/modify_time?phone=' + store.phone + '&order_id=' + this.data.productId + '&storeId=' + this.data.product.storeId,
        })
      }else{
        wx_alert.alert("请选择规格")
        return
      }

    }else{
      immediate = !immediate;
      this.setData({
        immediate: immediate
      })    
    }
  },
  close_bespoke:function(){
    var immediate = this.data.immediate;  
    immediate = !immediate;
    this.setData({
      immediate: immediate
    })      
  },
  // 单选
  choose_list:function(e){
    var index = e.currentTarget.dataset.index;
    var snList = this.data.snList;
    this.setData({
      choose_list:index,
      select_a: snList[index]
    })
  },
  // 多选
  choose_b:function(e){
    var index = e.currentTarget.dataset.index;
    var upList = this.data.upList;
    upList[index].selected = !upList[index].selected;

    if (upList[index].selected == false) {  
      for (var i in select_b){
        if (upList[index].id == select_b[i].id){
          select_b.splice(i,1)
        }
      }
    }    
    if (upList[index].selected == true) {
      select_b.push(upList[index])
    }
    this.setData({
      upList: upList,
      select_b: select_b
    })
    console.log(select_b)
  },
  call_phone: function (e) {
    console.log(e)
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    })
  },
  image_current:function(e){
    var index = e.currentTarget.dataset.index;
    var cont_img = this.data.cont_img;
    var img_url = this.data.img_url;
    var prev_image = []
    for (var i in cont_img){
      prev_image.push(img_url + cont_img[i]);
    }
    wx.previewImage({
      current: prev_image[index], // 当前显示图片的http链接
      urls: prev_image // 需要预览的图片http链接列表
    })     
  },
  time: function (product){
    var that = this;
    var _endTime ;
    //
    function FreshTime() {
      wx.getSystemInfo({
        success(res) {
          //  console.log(res)
          if (res.platform == "devtools") {
            _endTime = product.seckillTime;            
          } else if (res.platform == "ios") {

            _endTime = product.seckillTime.replace(/\-/g, '/');
                        
          } else if (res.platform == "android") {
            _endTime = product.seckillTime;            
          }
        }
      })


      var endtime = new Date(_endTime);//结束时间
      // console.log(endtime);
      var nowtime = new Date();//当前时间
      var lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000); // 剩余时间
      var d = parseInt(lefttime / 3600 / 24);   // 剩余天数
      var h = parseInt((lefttime / 3600) % 24); // 剩余小时数
      var m = parseInt((lefttime / 60) % 60); // 剩余分钟数
      var s = parseInt(lefttime % 60);    // 剩余秒数
      h < 10 ? h = '0' + h : h = h;
      m < 10 ? m = '0' + m : m = m;
      s < 10 ? s = '0' + s : s = s;
      
      // console.log(h + ":" + m + ":" + s)
      that.setData({
        hour: h,
        mintius:m,
        second:s
      })
    }
    FreshTime()
    var sh;
    sh = setInterval(FreshTime, 1000); // 每秒钟执行一次    
  },
  choose_address: function () {

    wx.openLocation({
      latitude: Number(this.data.store.lat),
      longitude: Number(this.data.store.lit),
      scale: 18
    })

  }

})

